package com.ae.shield.client.block.tileEntityRenderer;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.ae.shield.common.block.tile.NanoRobotProgrammerTile;
import com.ae.shield.common.items.EffectStorageItem;
import com.ae.shield.common.items.ItemList;
import com.ae.shield.common.items.shieldFittings.makers.NanoRobot;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.item.ItemStack;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.items.IItemHandler;

/**
 * 用于给方块添加额外要渲染的东西
 * 此处用于在方块上渲染储存的物品
 */
public class ProgrammerBlockTER extends TileEntityRenderer<NanoRobotProgrammerTile> {
    public ProgrammerBlockTER(TileEntityRendererDispatcher dispatcher) {
        super(dispatcher);
    }

    @Override
    public void render(NanoRobotProgrammerTile tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
        itemRender(tileEntityIn, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn);
    }

    public void itemRender(NanoRobotProgrammerTile tileEntityIn, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
        IItemHandler inventory = tileEntityIn.getInventory();
        Direction direction = tileEntityIn.getBlockState().get(BlockStateProperties.HORIZONTAL_FACING);
        byte facingRotation = 0;

        switch (direction) {
            case SOUTH:
                facingRotation = 2;
                break;
            case WEST:
                facingRotation = 3;
                break;
            case EAST:
                facingRotation = 1;
                break;
        }

        for (int i = 0; i < inventory.getSlots(); i++) {
            ItemStack stack = inventory.getStackInSlot(i);

            if (stack.isEmpty()) {
                continue;
            }

            matrixStackIn.push();

            if (stack.getItem() instanceof EffectStorageItem) {
                float x;
                float z;

                switch (i) {
                    case 1:
                        x = 0.797f;
                        z = -0.0875f;
                        break;
                    case 2:
                        x = 0.547f;
                        z = -0.0875f;
                        break;
                    case 3:
                        x = 0.422f;
                        z = 0.036f;
                        break;
                    case 4:
                        x = 0.2345f;
                        z = -0.025f;
                        break;
                    default:
                        x = 0f;
                        z = 0f;
                }

                for (int j = 0; j < facingRotation; j++) {
                    float z1 = z;
                    z = x;
                    x = 1 - z1;
                }

                matrixStackIn.translate(x, 0.0245d, z);
                matrixStackIn.scale(0.2f, 0.2f, 0.2f);
                matrixStackIn.rotate(new Quaternion(Vector3f.XN, 90f, true));
                stack = new ItemStack(ItemList.RENDER_ITEM.get(), 1);
            }

            if (stack.getItem() instanceof NanoRobot) {
                float z = 0.195f;
                float x = 0.11f;

                for (int j = 0; j < facingRotation; j++) {
                    float z1 = z;
                    z = x;
                    x = 1 - z1;
                }

                matrixStackIn.translate(x, 0.732d, z);
                matrixStackIn.scale(0.09f, 0.09f, 0.09f);
                matrixStackIn.rotate(new Quaternion(Vector3f.XN, 90f, true));
            }

            matrixStackIn.rotate(new Quaternion(Vector3f.ZN, facingRotation * 90, true));
            ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
            IBakedModel model = itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null);
            itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn, model);
            matrixStackIn.pop();
//            if (stack.getItem() instanceof NanoTool) {
//
//            } TODO 待纳米工具完成后添加特殊模型渲染
        }
    }
}